<template>
	<view class="detail-container">
		<!-- 详情卡片 -->
		<view class="detail-card">
			<view class="card-title">审计日志信息</view>
			<view class="detail-list">
				<view class="detail-item">
					<text class="item-label">实体类型</text>
					<text class="item-value">{{ formatEntityType(auditInfo.entityId?.entityType) }}</text>
				</view>
				
				<view class="detail-item">
					<text class="item-label">操作类型</text>
					<text class="item-value">{{ formatAction(auditInfo.actionType) }}</text>
				</view>
				
				<view class="detail-item">
					<text class="item-label">操作状态</text>
					<text class="item-value" :class="auditInfo.actionStatus === 'SUCCESS' ? 'success' : 'error'">
						{{ formatStatus(auditInfo.actionStatus) }}
					</text>
				</view>
				
				<view class="detail-item action-data-item">
					<text class="item-label">动作数据</text>
					<view class="action-data-value">
						<text>{{ formatActionData(auditInfo.actionData) }}</text>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
import request from '@/utils/request.js'

export default {
	data() {
		return {
			auditId: '',
			auditInfo: {},
			statusMap: {
				'SUCCESS': '成功',
				'FAILURE': '失败'
			},
			actionMap: {
				'ADDED': '已添加',
				'DELETED': '已删除',
				'UPDATED': '已更新',
				'ATTRIBUTES_UPDATED': '属性已更新',
				'ATTRIBUTES_DELETED': '属性已删除',
				'RPC_CALL': 'RPC调用',
				'CREDENTIALS_UPDATED': '凭证已更新',
				'ASSIGNED_TO_CUSTOMER': '已分配给客户',
				'UNASSIGNED_FROM_CUSTOMER': '已从客户取消分配',
				'ACTIVATED': '已激活',
				'SUSPENDED': '已暂停',
				'CREDENTIALS_READ': '已读取凭证',
				'ATTRIBUTES_READ': '已读取属性',
				'RELATION_ADD_OR_UPDATE': '关系已更新',
				'RELATION_DELETED': '关系已删除',
				'RELATIONS_DELETED': '所有关系已删除',
				'ALARM_ACK': '已确认',
				'ALARM_CLEAR': '已清除',
				'ALARM_DELETE': '告警已删除',
				'ALARM_ASSIGNED': '告警已分配',
				'ALARM_UNASSIGNED': '告警已取消分配',
				'LOGIN': '登录',
				'LOGOUT': '登出',
				'LOCKOUT': '已锁定',
				'ASSIGNED_FROM_TENANT': '已从租户分配',
				'ASSIGNED_TO_TENANT': '已分配给租户',
				'PROVISION_SUCCESS': '设备已配置',
				'PROVISION_FAILURE': '设备配置失败',
				'TIMESERIES_UPDATED': '遥测已更新',
				'TIMESERIES_DELETED': '遥测已删除',
				'ASSIGNED_TO_EDGE': '已分配给边缘',
				'UNASSIGNED_FROM_EDGE': '已从边缘取消分配',
				'ADDED_COMMENT': '已添加评论',
				'UPDATED_COMMENT': '已更新评论',
				'DELETED_COMMENT': '已删除评论',
				'SMS_SENT': '短信已发送'
			},
			entityTypeMap: {
				'TENANT': '租户',
				'TENANT_PROFILE': '租户配置',
				'CUSTOMER': '客户',
				'USER': '用户',
				'DASHBOARD': '仪表板',
				'ASSET': '资产',
				'DEVICE': '设备',
				'DEVICE_PROFILE': '设备配置',
				'ASSET_PROFILE': '资产配置',
				'ALARM': '告警',
				'RULE_CHAIN': '规则链',
				'RULE_NODE': '规则节点',
				'EDGE': '边缘',
				'ENTITY_VIEW': '实体视图',
				'WIDGETS_BUNDLE': '组件包',
				'WIDGET_TYPE': '组件类型',
				'API_USAGE_STATE': 'API使用状态',
				'TB_RESOURCE': '资源',
				'OTA_PACKAGE': 'OTA包',
				'RPC': 'RPC',
				'QUEUE': '队列',
				'NOTIFICATION_TARGET': '通知目标',
				'NOTIFICATION_TEMPLATE': '通知模板',
				'NOTIFICATION_REQUEST': '通知请求',
				'NOTIFICATION': '通知',
				'NOTIFICATION_RULE': '通知规则'
			}
		}
	},
	
	onLoad(options) {
		this.auditId = options.id
		
		// 获取上一页传递的数据
		const eventChannel = this.getOpenerEventChannel()
		eventChannel.on('auditData', (data) => {
			this.auditInfo = data.data
			// 设置导航栏标题
			uni.setNavigationBarTitle({
				title: this.auditInfo.entityName || '审计日志详情'
			})
		})
	},

	methods: {
		formatStatus(status) {
			return this.statusMap[status] || '-'
		},

		formatAction(action) {
			return this.actionMap[action] || '-'
		},

		formatEntityType(type) {
			return this.entityTypeMap[type] || '-'
		},

		formatActionData(data) {
			if (!data) return '-'
			try {
				return typeof data === 'object' 
					? JSON.stringify(data, null, 2)  // 使用2个空格进行缩进
					: data
			} catch (e) {
				return data
			}
		}
	}
}
</script>

<style>
.detail-container {
	min-height: 100vh;
	background-color: #f5f5f5;
	padding: 20rpx;
}

.detail-card {
	background-color: #fff;
	border-radius: 12rpx;
	padding: 30rpx;
}

.card-title {
	font-size: 32rpx;
	color: #333;
	font-weight: 600;
	margin-bottom: 30rpx;
}

.detail-list {
	display: flex;
	flex-direction: column;
	gap: 20rpx;
}

.detail-item {
	display: flex;
	justify-content: space-between;
	align-items: flex-start;
	padding: 16rpx 0;
	border-bottom: 1px solid #f5f5f5;
}

.detail-item:last-child {
	border-bottom: none;
}

.item-label {
	font-size: 28rpx;
	color: #666;
	min-width: 140rpx;
	margin-right: 20rpx;
}

.item-value {
	flex: 1;
	font-size: 28rpx;
	color: #333;
	text-align: right;
	word-break: break-all;
}

.item-value.success {
	color: #4CAF50;
}

.item-value.error {
	color: #F44336;
}

.action-data-item {
	flex-direction: column;
	align-items: flex-start;
}

.action-data-value {
	margin-top: 16rpx;
	width: 100%;
	padding: 20rpx;
	background-color: #F8F9FA;
	border-radius: 8rpx;
}

.action-data-value text {
	font-size: 26rpx;
	color: #333;
	text-align: left;
	white-space: pre-wrap;
	font-family: Consolas, monospace;
}
</style>
